# entity embedding full process
import argparse
import os
import shutil
import sys
import time
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parents[1]))
sys.path.append(str(Path(__file__).resolve().parents[0]))
from data_process import for_GAIA_process
from case_main import for_GAIA_emb
from case_vector import for_GAIA_vector
from for_graph import for_GAIA_graph
import pathlib

def entity_emb_main(config, train_case_name: list, test_case_name: list, if_train: bool):
    # case_name 训练的异常数据或者测试数据，训练和测试传入不同的数据
    dataset_choice = config['dataset']
    entity_emb_config = config['entity_embedding']
    # copy data:
    source = config['data_path']
    target = entity_emb_config['input_data_path']
    
    if os.path.exists(target):
        shutil.rmtree(target)
    try:
        shutil.copytree(source,target)
    except:
        print("Unable to copy file.",sys.exc_info())
        sys.exit()
    
    # data process:
    pathlib.Path(entity_emb_config['log_path']).mkdir(exist_ok=True, parents=True)
    normal_data_length = for_GAIA_process(target,entity_emb_config['log_path']+'/'+f'{dataset_choice}_process.txt', config, anomalies_list=train_case_name if if_train else test_case_name)
    print(f"{dataset_choice} data process √")
    
    
    # RE-GCN
    # train:
    pathlib.Path(entity_emb_config['output_data_path']).mkdir(exist_ok=True,parents=True)
    train_output_embedding = os.path.join(entity_emb_config['output_data_path'],'normal')
    if not os.path.exists(train_output_embedding):
        os.mkdir(train_output_embedding)
    if not os.path.exists(os.path.join(entity_emb_config['model_path'],entity_emb_config['model_name'])):
        command = "python3 {} --path_normal {}  -d normal  --model_name {} --model_path {} --nodes_list {} --n-hidden {} --n-layers {} --n-epochs {} --layer-norm --weight 0.5 --entity-prediction --relation-prediction --output_embedding {} --train-history-len {} --test-history-len {} > {} ".format(entity_emb_config['RE-GCN_main'],entity_emb_config['input_data_path'],entity_emb_config['model_name'],entity_emb_config['model_path'],entity_emb_config['nodes'],entity_emb_config['hidden_dimension'],entity_emb_config['gcn_layer'],entity_emb_config['epoch'],train_output_embedding,entity_emb_config['window_size'],entity_emb_config['window_size'],entity_emb_config['log_path']+'/'+dataset_choice+'_train.txt')
        res = os.system(command)
    print("Entity Embedding Model Train √")
    
    
    # 对case进行嵌入编码
    # case embedding:
    # confirm model:
    if not os.path.exists(entity_emb_config['model_path']+entity_emb_config['model_name']):
        raise Exception("Unable to embed!")
    # case embedding:
    for_GAIA_emb(entity_emb_config, train_case_name if if_train else test_case_name)
    print(f"{entity_emb_config['model_name']} cases embedding √ ")
    # vector combination:
    for_GAIA_vector(entity_emb_config, train_case_name if if_train else test_case_name)
    print(f"{entity_emb_config['model_name']} vector combination √ ")
    # for graph train and emb:
    for_GAIA_graph(entity_emb_config, config, train_case_name, test_case_name, if_train=if_train)
    print(f"{entity_emb_config['model_name']} vector to graph input files √ ")
    return normal_data_length


    
        


    




    



    
       

